#region File Description
//-----------------------------------------------------------------------------
// MenuScreen.cs
//
// XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion

#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using ScytheEngine;
#endregion

namespace ScytheEngine
{
    /// <summary>
    /// Base class for screens that contain a menu of options. The user can
    /// move up and down to select an entry, or cancel to back out of the screen.
    /// </summary>
	public abstract class MenuScreen : UIScreen
    {
        #region Fields

        string menuTitle;
		Vector2 m_MenuEntriesPosition = new Vector2( 100, 250 );

        #endregion

		#region Public Methods

		public void AddMenuEntry(MenuEntry menu_entry)
		{
			m_ScreenElements.Add( (UIScreenElement)menu_entry );
		}

		#endregion

		#region Properties


		/// <summary>
        /// Gets the list of menu entries, so derived classes can add
        /// or change the menu contents.
        /// </summary>
        protected IList<MenuEntry> MenuEntries
        {
            get 
			{
				List<MenuEntry> menu_entries = new List<MenuEntry>();
				foreach(UIScreenElement screen_element in m_ScreenElements)
				{
					menu_entries.Add( (MenuEntry)screen_element );
				}
				return menu_entries; 
			}
        }

		protected Vector2 MenuEntriesPosition
		{
			get { return m_MenuEntriesPosition; }
			set { m_MenuEntriesPosition = value; }
		}

		public MenuEntry SelectedMenuEntry
		{
			get { return (MenuEntry)m_SelectedScreenElement; }
			set { m_SelectedScreenElement = (UIScreenElement)value; }
		}


        #endregion

        #region Initialization

        /// <summary>
        /// Constructor.
        /// </summary>
        public MenuScreen(string menuTitle)
        {
            this.menuTitle = menuTitle;

            TransitionOnTime = TimeSpan.FromSeconds(0.5);
            TransitionOffTime = TimeSpan.FromSeconds(0.5);
        }

        #endregion

        #region Update and Draw


        /// <summary>
        /// Updates the menu.
        /// </summary>
        public override void Update(GameTime gameTime, bool otherScreenHasFocus,
                                                       bool coveredByOtherScreen)
        {
            base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);

            // Update each nested MenuEntry object.
			foreach ( UIScreenElement screen_element in m_ScreenElements )
			{
				MenuEntry menuEntry = (MenuEntry)screen_element;
				bool isSelected = IsActive && m_SelectedScreenElement == screen_element;

				menuEntry.Update( this, isSelected, gameTime );
            }
        }


        /// <summary>
        /// Draws the menu.
        /// </summary>
        public override void Draw(GameTime gameTime)
        {
            SpriteBatch spriteBatch = GameStateManager.SpriteBatch;
            SpriteFont font = GameStateManager.Font;

			Vector2 menu_entries_pos = m_MenuEntriesPosition;

            // Make the menu slide into place during transitions, using a
            // power curve to make things look more interesting (this makes
            // the movement slow down as it nears the end).
            float transitionOffset = (float)Math.Pow(TransitionPosition, 2);

            if (ScreenState == ScreenState.TransitionOn)
				menu_entries_pos.X -= transitionOffset * 256;
            else
				menu_entries_pos.X += transitionOffset * 512;

            spriteBatch.Begin();

            // Draw each menu entry in turn.
			foreach(UIScreenElement screen_element in m_ScreenElements)
			{
				MenuEntry menuEntry = (MenuEntry)screen_element;

                bool isSelected = IsActive && (screen_element == m_SelectedScreenElement);

				menuEntry.Draw( this, menu_entries_pos, isSelected, gameTime );

				menu_entries_pos.Y += menuEntry.GetHeight( this );
            }

            // Draw the menu title.
            Vector2 titlePosition = new Vector2(533, 80);
            Vector2 titleOrigin = font.MeasureString(menuTitle) / 2;
            Color titleColor = new Color(192, 192, 192, TransitionAlpha);
            float titleScale = 1.25f;

            titlePosition.Y -= transitionOffset * 100;

            spriteBatch.DrawString(font, menuTitle, titlePosition, titleColor, 0,
                                   titleOrigin, titleScale, SpriteEffects.None, 0);

            spriteBatch.End();
        }


        #endregion
    }
}
